import React, {Component} from 'react'
import {connect} from 'react-redux';
import {userLogin} from 'react-admin';
import dd from 'dingtalk-jsapi';
import {httpGet} from '../fetchUtils';
import cryptoJS from 'crypto-js';
import '../css/login.less';
class Login extends Component {

    constructor(props) {
        super(props);
        // Login.requestAuthCode();
    }

    getToken() {
        httpGet("gettoken?corpid=ding582f7345dfba513735c2f4657eb6378f" +
            "&corpsecret=xMnS3lnMLCz0cvGPSC-T_gsosneyTGTUs2mjLoYggE5vzz_uFmg0dZMnMijAr1ur",
            function (response) {
                if (response.errcode === 0) {
                    // alert(response.access_token);
                    Login.requestAuthCode(response.access_token);
                }
                return "";
            })
    }

    // static getJsticket(token) {
    //     httpGet("get_jsapi_ticket?access_token=" + token,
    //         function (response) {
    //             if (response.errcode === 0) {
    //                 let timestamp = Date.parse(new Date());
    //                 let nonceStr = "Zn4zmLFKD0wzilzM";
    //                 let url = "http://open.dingtalk.com";
    //                 let jsApiSingnature = Login.getJsApiSingnature(response.ticket, nonceStr,
    //                     timestamp, url);
    //                 dd.config({
    //                     agentId: "215006665",
    //                     corpId: "ding582f7345dfba513735c2f4657eb6378f",
    //                     timeStamp: timestamp, // 必填，生成签名的时间戳
    //                     nonceStr: nonceStr, // 必填，生成签名的随机串
    //                     signature: jsApiSingnature, // 必填，签名
    //                     jsApiList: [                              //需要调用的借口列表
    //                         'runtime.permission.requestAuthCode',
    //                         'getAuthCode'
    //                     ]
    //                 });
    //                 Login.testJsApi();
    //             }
    //         })
    // }

    // static getJsApiSingnature(ticket, nonce, timeStamp, url) {
    //     let plainTex = "jsapi_ticket=" + ticket + "&noncestr=" + nonce + "&timestamp=" +
    //         timeStamp + "&url=" + url;
    //     return cryptoJS.SHA1(plainTex).toString();
    // }

    static requestAuthCode() {
        dd.ready(() => {
            dd.runtime.permission.requestAuthCode({
                corpId: "ding582f7345dfba513735c2f4657eb6378f",
                onSuccess: function (result) {
                     alert(result.code);
                    //todo 根据授权码跟token获取userID
                    // httpGet("user/getuserinfo?access_token=" + token + "&code=" + result.code,
                    //     function (response) {
                    //         alert(response.userid);
                    //     })
                },
                onFail: function (err) {
                    alert(JSON.stringify(err))
                }

            })
        });
    }

    componentDidMount() {

    }

    render() {
        return (
            <div id="login_container" style={{
                width: '100%', height: '200px', position: 'absolute',
                top: '35%', textAlign: 'center'
            }}>
                <div className="login-dev">222</div>
            </div>
        )
    }
}

export default connect(undefined, {userLogin})

(
    Login
)
;